package com.teach.controller.shop;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.dto.LoginUser;
import com.teach.dto.ShopProductSearch;
import com.teach.entity.Product;
import com.teach.entity.ShopAddress;
import com.teach.entity.ShopCart;
import com.teach.mapper.ShopAddressMapper;
import com.teach.mapper.ShopCartMapper;
import com.teach.service.shop.ShopAddressService;
import com.teach.service.shop.ShopGoodsService;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class ShopGoodsController {

	@Autowired
	private ShopCartMapper mapper;
	
	@Autowired
	private ShopGoodsService service;
	
	@Autowired
	private ShopAddressService service1;
	
	@Autowired
	private ShopAddressMapper ampper;
	//添加购物车
	@PostMapping("/add-cart.do")
	@ResponseBody
	public Object doShop(LoginUser user,Long productId,Integer goodsnum,Float goodsprice,String goodsname) {
			ShopCart list=mapper.selectByprouctId(productId,user.getUserName());
			if(list!=null) {
				Integer num=list.getShopCartNum()+goodsnum;
				Float price=list.getShopCartProductPrice()+goodsprice;
				mapper.updateByprouctId(productId, user.getUserName(), num,price);
			}else {
				ShopCart cart=new ShopCart();
				cart.setShopCartUserName(user.getUserName());
				cart.setShopCartProductId(productId);
				cart.setShopCartNum(goodsnum);
				cart.setShopCartProductPrice(goodsprice);
				cart.setShopCartGoodsName(goodsname);
				mapper.insertSelective(cart);
			}
			return 1;
	}
	
	//购买商品
	@PostMapping("/goods-by.do")
	public String buy(Model model,Long id,Integer num,LoginUser user) {
		ShopAddress s=ampper.selectss(user.getUserName());
		if(s!=null) {
			model.addAttribute("addressList", service1.get(user.getUserName()));
			
			log.debug("{}",s);
			Product pro=service.get(id);
			model.addAttribute("products", pro);
			model.addAttribute("num", num);
			model.addAttribute("total", pro.getProductPrice1()*num);
			return "shop/order";
		}else {
			return "forward:addAddresss.page";
		}
	}
	
	
	@PostMapping("/addAddresss.page")
	public String doAddAddress() {
		return "shop/userAddAddress";
	}
	
	@GetMapping("/product-{current}.do")
	public String page(Model model,@PathVariable Integer current,String name,String productClass,Integer p,Integer sales,Float productPrice,Integer time) {
		ShopProductSearch search=new ShopProductSearch();
		search.setProductName(name);
		search.setCurrent(current);
		search.setProductClass(productClass);
		search.setSales(sales);
		search.setProductPrice(productPrice);
		search.setTime(time);
		model.addAttribute("pai", service.selectByName(name));
		model.addAttribute("display", service.page(search));
		model.addAttribute("sstj", name);
		model.addAttribute("productPrice", productPrice);
		model.addAttribute("sales", sales);
		model.addAttribute("time", time);
		model.addAttribute("p", p);
		return "shop/search";
	}

}
